/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.administration;

import static controller.Connexion.CONF_DAO_INIT;
import static controller.administration.AssociationCours.VUE_ADMINISTRATION_ACCUEIL;
import dao.DAOInit;
import dao.interfaces.DAOCours;
import dao.interfaces.DAOEtudiant;
import dao.interfaces.DAOExamen;
import dao.interfaces.DAONote;
import gestionform.GestionCours;
import gestionform.GestionEtudiant;
import gestionform.GestionExamen;
import gestionform.GestionNote;
import gestionform.GestionSaisieNotes;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.Cour;
import model.Enseignant;
import model.Etudiant;
import model.Examen;
import model.Personne;

/**
 *
 * @author Paul
 */
public class SaisieNotes extends HttpServlet {

    public static final String ACCES_CONNEXION = "/Connexion";
    public static final String VUE_SAISIE_NOTES = "/WEB-INF/administration/saisieNotes.jsp";
    public static final String ATT_GESTION_SAISIE_NOTES = "gSaisieNotes";
    public static final String ATT_GESTION_COURS = "gCours";
    public static final String ATT_GESTION_ETUDIANT = "gEtudiant";
    public static final String ATT_GESTION_EXAMEN = "gExamen";
    public static final String ATT_GESTION_NOTE = "gNote";
    private DAOCours daoCours;
    private DAOEtudiant daoEtudiant;
    private DAOExamen daoExamen;
    private DAONote daoNote;

    @Override
    public void init() throws ServletException {
        /* Récupération d'une instance de notre DAO Personne */
        this.daoCours = ((DAOInit) getServletContext().getAttribute(CONF_DAO_INIT)).getDAOCours();
        this.daoEtudiant = ((DAOInit) getServletContext().getAttribute(CONF_DAO_INIT)).getDAOEtudiant();
        this.daoExamen = ((DAOInit) getServletContext().getAttribute(CONF_DAO_INIT)).getDAOExamen();
        this.daoNote = ((DAOInit) getServletContext().getAttribute(CONF_DAO_INIT)).getDAONote();
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Enseignant ens = (Enseignant) request.getSession().getAttribute("enseignant");
        if (ens != null) {
            if (request.getParameter("retourAdministration") != null) {
                this.getServletContext().getRequestDispatcher(VUE_ADMINISTRATION_ACCUEIL).forward(request, response);
            }  else {
                GestionSaisieNotes gsn = new GestionSaisieNotes();
                HttpSession session = request.getSession(true);
                GestionCours gCours = new GestionCours(daoCours);
                ArrayList<Cour> listeCours = gCours.getListeCoursEnseignant(ens.getId());
                if (!gCours.getErreurs().isEmpty()) {
                    request.setAttribute(ATT_GESTION_COURS, gCours);
                } else {
                    session.setAttribute("listeCours", listeCours);
                    GestionExamen gExamen = new GestionExamen(daoExamen);
                    ArrayList<Examen> listeExamens = new ArrayList<Examen>();
                    for (Cour cour : listeCours) {
                        listeExamens.addAll(gExamen.getExamens(cour.getIdCour()));
                    }
                    if (!gExamen.getErreurs().isEmpty()) {
                        request.setAttribute(ATT_GESTION_EXAMEN, gExamen);
                    } else {
                        session.setAttribute("listeExamens", listeExamens);
                    }
                }
                GestionEtudiant gEtudiant = new GestionEtudiant(daoEtudiant);
                ArrayList<Etudiant> listeEtudiants = gEtudiant.getAllEtudiants();
                if (!gEtudiant.getErreurs().isEmpty()) {
                    request.setAttribute(ATT_GESTION_ETUDIANT, gEtudiant);
                } else {
                    session.setAttribute("listeEtudiants", listeEtudiants);
                }
                request.setAttribute(ATT_GESTION_SAISIE_NOTES, gsn);
                this.getServletContext().getRequestDispatcher(VUE_SAISIE_NOTES).forward(request, response);
            }

        } else {
            request.getRequestDispatcher("view/connexion.jsp").forward(request, response);
        }
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Enseignant ens = (Enseignant) request.getSession().getAttribute("enseignant");
        GestionSaisieNotes gsn = new GestionSaisieNotes();
        gsn.initParamatersValues(request);
        if (gsn.getErreurs().isEmpty()) {
            GestionNote gNote = new GestionNote(daoNote);
            String cours = gsn.getCours();
            String examen = gsn.getExamen();
            String etudiant = gsn.getEtudiant();
            String note = gsn.getNote();
            gNote.affecterNote(cours, examen, etudiant, note);
            request.setAttribute(ATT_GESTION_NOTE, gNote);
        }
        request.setAttribute(ATT_GESTION_SAISIE_NOTES, gsn);
        this.getServletContext().getRequestDispatcher(VUE_SAISIE_NOTES).forward(request, response);
    }
}
